﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class MembershipCard_wechat_web_ShopCar2 :WeiPage
{
    ShoppingCartBO sbo = new ShoppingCartBO();
    PersonInfoBO fbo = new PersonInfoBO();
    PRODUCTBO pbo = new PRODUCTBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret="+GetSecret()+"&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                    }
                }
                catch (Exception)
                {

                }
            }
            if (Request.QueryString["sid"] != null)
            {
                sbo.Delete(int.Parse(Request.QueryString["sid"].ToString()));
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                UploadCar();
            }
        }
    }

    private void UploadCar()
    {
        string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
        string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
        Session["FromUserName"] = user;
        Session["ToUserName"] = user2;
        PersonInfo per = fbo.GetModel(user);
        if (per != null)
        {
            List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
            if (list.Count > 0)
            {
                sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
            }
            PRODUCT p = new PRODUCT();
            int sumnum = 0; allcheck.InnerHtml = "";
            foreach (ShoppingCart c in list)
            {
                p = pbo.GetModel(int.Parse(c.RID.ToString()));
                if (p != null)
                {
                    allcheck.InnerHtml += string.Format(@"<div class='order_pin'>
    <p><a href='#'><img src='{0}' /></a><!--<span style='float:left;'><div style='width:60%;text-align:right;'><a href='#'  onclick='return sc({5})'>x</a></div><span>--><span>{1}</span>
    <span>  价格：<bdo>{2}元</bdo></span>

    </p>
      <div class='zongji'><span>数量：<input name='' onchange='textchange({5},this.value)' type='number'   value='{3}' size='3' class='input'/> &nbsp;共计：<bdo>{4}元</bdo>&nbsp;&nbsp; <a href='#'  onclick='return sc({5})'>删除</a></span><label><input name='{4}' type='checkbox' checked='checked' value='{5}'  class='dropdown-checkbox checkboxcur' onclick='javascript:checks(this);'/></label></div>
  </div>", (p.PicUrl.Trim() == "" ? "images/m.png" : p.PicUrl), p.Name, p.Price, c.RNum, c.RNum * p.Price, c.ID);
                    sumnum += int.Parse(c.RNum.ToString()) * int.Parse(p.Price.ToString());
                }
            }
            if (list.Count == 0)
            {
                allcheck.InnerHtml = "<div style='text-align:center; color:Gray;'><br /><br /><br /><br /><br /><br />无记录</div>";
                js.Visible = false;
            }
            sumfen.InnerHtml = sumnum.ToString() + "元";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            try
            {
                ShoppingCart c = sbo.GetModel(int.Parse(carid.Value));
                if (c != null)
                {
                    c.RNum = int.Parse(carnum.Value);
                    sbo.Update(c);
                    UploadCar();
                }
            }
            catch (Exception)
            {

            }
        }

    }
}